<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">

    <body>

        <ui:composition template="../templates/default3.xhtml">
            <ui:define name="content">
                <h:form binding="#{creditManagerBean.loadCreditCheck}">
                    <p:panel header="Customer Details" id="customerDetailsPanel">
                        <table width="100%" border="0">
                            <tr>
                                <td width="200px">
                                    Company:
                                </td>
                                <td>
                                    <h:outputText id="company" value="#{creditManagerBean.selectedCustomer.company}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Description:
                                </td>
                                <td>
                                    <h:outputText id="description" value="#{creditManagerBean.selectedCustomer.description}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Annual Revenue:
                                </td>
                                <td>
                                    <h:outputText id="revenue" value="#{creditManagerBean.selectedCustomer.revenue}">
                                        <f:convertNumber type="currency"/>
                                    </h:outputText>
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Number of Employees:
                                </td>
                                <td>
                                    <h:outputText id="numEmployee" value="#{creditManagerBean.selectedCustomer.numEmployee}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Contact Person
                                </td>
                                <td>
                                    <h:outputText id="contactPerson" value="#{creditManagerBean.selectedCustomer.firstName}, 
                                                  #{creditManagerBean.selectedCustomer.lastName}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Designation:
                                </td>
                                <td>
                                    <h:outputText id="title" value="#{creditManagerBean.selectedCustomer.title}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Contact Number:
                                </td>
                                <td>
                                    <h:outputText id="phone" value="#{creditManagerBean.selectedCustomer.phone}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Credit Rank:
                                </td>
                                <td>
                                    <h:outputText id="rank" value="#{creditManagerBean.selectedCustomer.credit.creditRank}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Credit Limit:
                                </td>
                                <td>
                                    <h:outputText id="limit" value="#{creditManagerBean.selectedCustomer.credit.creditLimit}">
                                        <f:convertNumber type="currency"/>
                                    </h:outputText>
                                </td>
                            </tr>
                        </table>
                    </p:panel>
                    <p:panel header="Order Details" id="orderDetailsPanel">
                        <p:messages/>

                        <p:dataTable id="orderItems" var="orderItem" value="#{creditManagerBean.salesOrders}" 
                                     emptyMessage="No sales orders found with given criteria."
                                     selection="#{creditManagerBean.selectedSalesOrder}"
                                     selectionMode="single"
                                     rowSelectListener="#{creditManagerBean.onRowSelect}"
                                     onRowSelectUpdate="creditCheckResult" 
                                     rowUnselectListener="#{creditManagerBean.onRowUnselect}"
                                     onRowUnselectUpdate="creditCheckResult">
                            <p:column filterBy="#{orderItem.job.purchaseOrder.id}" headerText="PO ID" 
                                      filterMatchMode="contains" sortBy="#{orderItem.job.purchaseOrder.id}">  
                                <h:outputText value="#{orderItem.job.purchaseOrder.id}"/> 
                            </p:column> 
                            <p:column headerText="Total Amount" sortBy="#{orderItem.job.purchaseOrder.totalAmount}">  
                                <h:outputText value="#{orderItem.job.purchaseOrder.totalAmount}">  
                                    <f:convertNumber type="currency"/>
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Credit Rate (%)" sortBy="#{orderItem.job.purchaseOrder.creditRate}">  
                                <h:outputText value="#{orderItem.job.purchaseOrder.creditRate}">
                                </h:outputText>
                            </p:column> 
                        </p:dataTable>
                    </p:panel>
                    <p:panel header="Credit Check Result" id="creditCheckResult">
                        <p:inputTextarea id="result" style="width:815px;height:150px;overflow:scroll" maxHeight="150"
                                         effectDuration="400" readonly="true" value="#{creditManagerBean.result}"
                                         title="Credit check result"/>
                        <p:commandButton id="btnPassCreditCheck" value="Pass Credit Check" 
                                         onclick="confirmation2.show()" type="button"
                                         title="Pass this sales order's credit check."
                                         disabled="#{creditManagerBean.isNullSalesOrder or creditManagerBean.hasOutstandingPayments}"/>
                        <p:commandButton id="btnFailCreditCheck" value="Fail Credit Check" 
                                         onclick="confirmation.show()" type="button"
                                         title="Fail this sales order's credit check."
                                         disabled="#{creditManagerBean.isNullSalesOrder}"/>
                    </p:panel>
                    <p:confirmDialog  width="400" message="" header="Failing a credit check" severity="alert" widgetVar="confirmation">
                        <f:facet name="message">
                            <h:outputText value="Failing this sales order will stop any related processes for the order."/><br/>
                            <h:outputText value="Are you sure you want to continue?"/>
                        </f:facet>    
                        <p:commandButton value="Yes" update="orderDetailsPanel" onclick="confirmation.hide()"
                                         actionListener="#{creditManagerBean.failCreditCheck}"/>
                        <p:commandButton value="No" update="orderDetailsPanel" onclick="confirmation.hide()" type="button" />
                    </p:confirmDialog>
                    <p:confirmDialog  width="400" message="" header="Passing a credit check" severity="alert" widgetVar="confirmation2">
                        <f:facet name="message">
                            <h:outputText value="Passing this sales order's credit check will trigger the ATP Check for this order."/><br/>
                            <h:outputText value="Are you sure you want to continue?"/>
                        </f:facet>    
                        <p:commandButton value="Yes" update="orderDetailsPanel" onclick="confirmation2.hide()"
                                         actionListener="#{creditManagerBean.passCreditCheck}" ajax="true"/>
                        <p:commandButton value="No" update="orderDetailsPanel" onclick="confirmation2.hide()" type="button" />
                    </p:confirmDialog>
                </h:form>
                <p:ajaxStatus onstart="statusDialog.show();" onsuccess="statusDialog.hide();"/>  
                <p:dialog modal="true" widgetVar="statusDialog" header="Please wait..."   
                          draggable="false" closable="false" resizable="false">  
                    <p:graphicImage value="../resources/images/ajaxloadingbar.gif" />  
                </p:dialog>
            </ui:define>
        </ui:composition>
    </body>
</html>
